﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        window.onresize=calSize;
        var rowEditing = -1;
        var fieldEditing = -1;
        var columnsList = [];
        var editing = false;
        var cusId=GetQueryString("cusid");
		var titles = getTitle();
        var customer = dataFetcher("../../api/customer/get?id=" + cusId);
        var productions=getProduction();
		var employees =getEmployee();
		var departments = getDepartment();
		var employeesInSP=getEmployeeBySalesPoint();
        document.onkeydown = keyDown;
        $(document).ready(function(){
			initBase();
            listDetail();
            calSize();
            getColumnVisable();
		});
        function getColumnVisable(){
            var allColumn = $('#customerOtherList').datagrid('getColumnFields');
            for(var j=0;j<allColumn.length;j++){
                var col=$('#customerOtherList').datagrid('getColumnOption',allColumn[j]);
                if(!(col.hidden)&&(col.editor!=undefined)){
                    columnsList.push(allColumn[j]);
        		}
            }
        }
        function initBase(){
            $('#Code').textbox('setValue', customer.Code);
            $('#Name').textbox('setValue', customer.Name);
			var counter=0;
			for(var i=0;i<employees.length;i++){
				if(counter==2){break;}
				if(employees[i].AutoId == customer.SalesId){
					$('#SalesId').textbox('setValue', employees[i].Name+'('+valToName(titles,employees[i].TitleId)+')');
					counter=counter+1;
				}
				if(employees[i].AutoId == customer.SecSalesId){
					$('#SecSalesId').textbox('setValue', employees[i].Name+'('+valToName(titles,employees[i].TitleId)+')');
					counter=counter+1;
				}
			}
            $('#Source').textbox('setValue', valToName(customerSourceData, customer.Source));
            $('#Receiveable').numberbox('setValue', customer.Receiveable);         
			$('#OtherBonus').numberbox('setValue', customer.OtherBonus);			
            $('#Received').numberbox('setValue', customer.Received);      
            $.get("../../api/salespoint/get?id=" + customer.SalesPointId,function(data){if(data.Result==0)$('#SalesPointId').textbox('setValue',(JSON.parse(data.Data)).Name);},"json");
        }
        function updateMoney(){
             customer = dataFetcher("../../api/customer/get?id=" + cusId);
            $('#Received').numberbox('setValue', customer.Received);             
            $('#Receiveable').numberbox('setValue', customer.Receiveable);
            $('#OtherBonus').numberbox('setValue', customer.OtherBonus);
        }
        function listDetail(){
            $('#customerOtherList').datagrid('loading');
            $.get("../../api/customerOther/listbycustomer?customer="+cusId,function(data){
                if(data.Result==0){
					$('#customerOtherList').datagrid({data: JSON.parse(data.Data)}).datagrid('clientPaging'); 
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
                $('#customerOtherList').datagrid('loaded');
            });
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-35;
            $("#customerOtherList").datagrid('resize',{height:height-205,width:width-20});
            $("#toolbar").panel({width:width-20});
            $("#editZone").panel({width:width-20});
        }
        function addItem() {
			var date = new Date().toLocaleDateString();
			if(rowEditing>0){ $('#customerOtherList').datagrid("endEdit",rowEditing); }
			dataCache = { AutoId: 0, Flag: 0, Code:getAutoCode('EXP_CUSTOMEROTHER_SET'),Date: date,State:0,CustomerId: cusId,Amount:0 };
			$('#customerOtherList').datagrid('appendRow', dataCache);
        }
		function rowSelected(index,row){
			if(rowEditing!=index){
				$('#customerOtherList').datagrid('validateRow', rowEditing);
				$('#customerOtherList').datagrid("endEdit",rowEditing)
				rowEditing = index;
				dataCache = row;
			}
			if(row.State==1||row.State==0){
				if(!editing){
					$('#customerOtherList').datagrid("enableCellEditing");
					editing = true;
				}
			}else{
				if(editing){
					$('#customerOtherList').datagrid("cancelEdit",index);
                    $('#customerOtherList').datagrid("disableCellEditing");
					editing = false;
				}
			}
			updateBtnState(dataCache.State);
		}
        function productionFormater(value, row, index){
            return valToName(productions,value);
        }
        function employeeFormater(value, row, index){
            return valToName(employees,value);
        }
		function departmentFormatter(value, row, index){
            return valToName(departments,value);
        }
		function titleFormatter(value, row, index){
            return valToName(titles,value);
        }
		function typeFormatter(value, row, index){
            return valToName(otherType,value);
        }
        function submitChange(){
            showMask();
            var valid = $('#customerOtherList').datagrid('validateRow', rowEditing);
            if (!valid) {
                $.messager.alert("错误", "错误的数据格式", "error");
                return;
            }
            $('#customerOtherList').datagrid('endEdit', rowEditing);
            dataCache = $('#customerOtherList').datagrid('getSelected');
            $.post("../../api/customerother/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
					$('#customerOtherList').datagrid('updateRow',{index:rowEditing,row:dataCache});
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 	
                    updateBtnState(dataCache.State);                                  
                    $.messager.alert("信息","操作成功","info");
                }else{
                    $.messager.alert("错误",response.Message,"error");
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
                hideMask();
            });
        }
		function deleteRecord(){
			var selected = $('#customerOtherList').datagrid('getSelected');
			if(!selected){
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }else{
				dataCache = selected;
			}
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					
					if(dataCache.AutoId&&dataCache.AutoId>0){		
						showMask();					
						$.post("../../api/customerother/remove",dataCache,function(response){
							if(response.Result==0){				
								$.messager.alert("信息","操作成功","info");
								$('#customerOtherList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
								rowEditing = -1;
							}else{
								$.messager.alert("错误",response.Message,"error");
							}
						})
						.error(function(){
							$.messager.alert("错误","系统内部错误","error");
						})
						.complete(function() { 
							hideMask();
						});	
					}else{
						$('#customerOtherList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
						rowEditing = -1;
					}

				}
			});		
		}
		function submitRecord(){
			showMask();
			dataCache = $('#customerOtherList').datagrid('getSelected' ); 
			$.get("../../api/customerother/submit?id="+dataCache.AutoId,function(response){
				if(response.Result==0){			
					dataCache.State = 2;
					$('#customerOtherList').datagrid('updateRow',{index:rowEditing,row:dataCache} ); 
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 		
                    updateBtnState(dataCache.State);
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
		function recallRecord(){
			showMask();
			dataCache = $('#customerOtherList').datagrid('getSelected' ); 
			$.get("../../api/customerother/recall?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#customerOtherList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 	
                    updateBtnState(dataCache.State);                    
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function approveRecord(){
			showMask();
			dataCache = $('#customerOtherList').datagrid('getSelected' ); 
			$.get("../../api/customerother/approve?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 3;
					$('#customerOtherList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 		
                    updateBtnState(dataCache.State);
                    updateMoney();
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function revertRecord(){
			showMask();
			dataCache = $('#customerOtherList').datagrid('getSelected' ); 
			$.get("../../api/customerother/revert?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#customerOtherList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 		
                    updateBtnState(dataCache.State);
                    updateMoney();
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function rejectRecord(){
			showMask();
			dataCache = $('#customerOtherList').datagrid('getSelected' ); 
			$.get("../../api/customerother/reject?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#customerOtherList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#customerOtherList').datagrid('refreshRow',rowEditing ); 		
                    updateBtnState(dataCache.State);
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
        function setCellEditable(){
            $('#customerOtherList').datagrid('validateRow', rowEditing);
            $('#customerOtherList').datagrid('endEdit', rowEditing);
            $('#customerOtherList').datagrid('selectRow', rowEditing).datagrid('editCell', {index: rowEditing,field: fieldEditing});
        }
        function keyDown(event) {
            if(!editing){
                return;
            }
            var e = event || window.event;
            var keyCode = e.keyCode || e.which;
            switch (keyCode) {
                case 37:
                    hMove(-1);
                    break;
                case 39:
                    hMove(1);
                    break;
            }
        }
        function hMove(direction){
            if(rowEditing==-1){
                return;
            }
            if(fieldEditing==-1){
                fieldEditing = columnsList[0];
            }
            var ed=$('#customerOtherList').datagrid('getEditor', {index:rowEditing,field:fieldEditing});
            if(!ed){
                return;
            }
            if(direction>=0){
                direction=1;
            }
            else{
                direction=-1;
            }
            var tag = null;
            if($(ed.target).data('textbox')){
                tag = $(ed.target).textbox('textbox')[0];
            }
            var valueLenth=tag.value.length;
            if(tag==null||tag.readOnly||(tag.selectionStart==0 || tag.selectionStart==undefined)&&direction<0
                ||(tag.selectionEnd==valueLenth || tag.selectionEnd==undefined)&&direction>=0){
                for(var i=0;i<columnsList.length;i++){
                    if(fieldEditing==columnsList[i] && (i+direction)>=0 && (i+direction)<columnsList.length){
                        fieldEditing=columnsList[(i+direction)];
                        break;
                    }
                }
                setCellEditable();
            }
        }
        function setEditingLocation(index, field){
            if(!editing){
                return;
            }
            var editor = $('#customerOtherList').datagrid('getEditor', { index: index, field: field });
            if(editor!=null){
                rowEditing=index;
                fieldEditing=field;
            }
        }
		function setTitleAndDepartment(n,o){
			var row = $('#customerOtherList').datagrid('getSelected');
			for(var i=0;i<employees.length;i++){
				if(employees[i].AutoId==n){
					row.TitleId = employees[i].TitleId;
					row.DepartmentId = employees[i].DepartmentId;
					return;
				}
			}
		}
		
    </script>
</head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
    <div id="toolbar" class="easyui-panel" style="padding:1px;">
        <a href="#" id="detailAdd" class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true">创建</a>
        <a href="#" id="detailDel" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true,disabled:true">删除</a>
        <a href="#" id="detailSave" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true,disabled:true">保存</a>
        <a href="#" id="btnSubmit" class="easyui-linkbutton" data-options="onClick:submitRecord,iconCls:'icon-redo',plain:true,disabled:true">提审</a>
        <a href="#" id="btnRecall" class="easyui-linkbutton" data-options="onClick:recallRecord,iconCls:'icon-undo',plain:true,disabled:true">撤回</a>
        <a href="#" id="btnApprove" class="easyui-linkbutton" data-options="onClick:approveRecord,iconCls:'icon-ok',plain:true,disabled:true">审核</a>
        <a href="#" id="btnRevert" class="easyui-linkbutton" data-options="onClick:revertRecord,iconCls:'icon-cancel',plain:true,disabled:true">反审</a>
        <a href="#" id="btnReject" class="easyui-linkbutton" data-options="onClick:rejectRecord,iconCls:'icon-clear',plain:true,disabled:true">退回</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:function(){exportData('../../api/customerother/export',[{CustomerId:cusId}])} ,iconCls:'icon-pencil',plain:true">导出</a>
    </div>
    <div style="margin:2px"></div>
    <div id="editZone" class="easyui-panel" style="padding:10px;">
        <table>
            <tr>
                <td><input class="easyui-textbox" id="Code" style="width:300px" data-options="label:'客户编号',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="Name" style="width:300px" data-options="label:'客户名称:',editable:false"></td>
				<td>&nbsp;</td>
                <td><input class="easyui-textbox" id="SalesPointId" style="width:300px" data-options="label:'门店',editable:false"></td>
                
            </tr>
            <tr>
                <td><input class="easyui-textbox" id="Source" style="width:300px" data-options="label:'来源',editable:false"></td>
				<td>&nbsp;</td>
                <td><input class="easyui-textbox" id="SalesId" style="width:300px" data-options="label:'业务员:',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="SecSalesId" style="width:300px" data-options="label:'导购:',editable:false,"></td>
            </tr>
            <tr>
                <td><input class="easyui-numberbox" id="Received" style="width:300px" data-options="label:'收款金额',editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-numberbox" id="Receiveable" style="width:300px" data-options="label:'应付金额:',editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
				<td>&nbsp;</td>
				<td><input class="easyui-numberbox" id="OtherBonus" style="width:300px" data-options="label:'其他费用',editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
            </tr>
        </table>
    </div>
    <div style="margin:2px"></div>                        
    <table id="customerOtherList" class="easyui-datagrid"  data-options="singleSelect:true,method:'get',onClickRow:rowSelected,pagination:true,rownumbers:true,onClickCell:setEditingLocation">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'CustomerId',hidden:true"></th>
                <th data-options="field:'Code',width:100,align:'right',editor:{type:'textbox'}">费用编号</th>
                <th data-options="field:'EmployeeId',width:120,align:'right',
                        formatter:employeeFormater,
						editor:{
							type:'combogrid',
							options:
									getComboGridInfo(null,'Name',[[
									{field:'AutoId',title:'ID',width:60},
									{field:'Code',title:'代号',width:60},
									{field:'Name',title:'姓名',width:100}]],employees,setTitleAndDepartment)
						}">支付对象*</th>
				<th data-options="field:'DepartmentId',width:100,formatter:departmentFormatter,editor:{type:'combobox',options:{valueField:'AutoId',textField:'Name',data:departments}}">部门</th>
				<th data-options="field:'TitleId',width:100,formatter:titleFormatter">职等</th>
				<th data-options="field:'Type',width:100,formatter:typeFormatter,editor:{type:'combobox',options:{valueField: 'AutoId',textField: 'Name',data:otherType}}">费用类型*</th>
                <th data-options="field:'DocDate',width:100,align:'right',editor:{type:'datebox',options:{formatter:dateFormatter,parser:dateParser}}">日期*</th>
                <th data-options="field:'Amount',width:100,align:'right',formatter:moneyFormatter,editor:{type:'numberbox',options:{precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'}}">金额*</th>
                <th data-options="field:'Note',width:150,align:'right',editor:{type:'textbox'}">备注</th>
                <th data-options="field:'State',width:100,formatter:stateFormatter">状态</th>
			</tr>
		</thead>
    </table>
</body>
</html>